package com.lucenten.collect.gnss.handler.tools;

import cn.quevo.common.lang.DateFormatUtils;
import cn.quevo.common.lang.NumberUtils;
import com.lucenten.collect.gnss.entity.GnssFile;

import java.util.Calendar;

public class TecDateFormat {

    /**
     * 格式化时间
     * @param line 时间字符串
     * @param end 分钟截止位置
     * @param year 实际年份(yyyy)
     * @return
     */
    public static String format(String line,int end, String year){
        String[] args = line.substring(0,end).trim().replaceAll(" +"," ").split(" ");
        StringBuilder date = new StringBuilder();
        // 年 yyyy
        date.append(args[0].length() == 4 ? args[0] : (year.substring(0,4-args[0].length()).concat(args[0])));
        //MMddHHmm
        for (int i = 1; i< 5; i ++){
            date.append(args[i].length() == 2 ? args[i] : "0".concat(args[i]));
        }
//        date = date.length() == 10 ? year.substring(0,2) + date : date.length() == 11 ? year.substring(0,1) + date : date;
        // 计算秒(因为秒数截取后有小数点)
        int s = (int) Math.round(NumberUtils.toDouble(line.substring(end).trim()));
        if (s < 10) {
            return date.append("0").append(s).toString();
        }
        return date.append(s).toString();
    }

    /**
     * 计算周秒(当前时间是当前星期的秒数)
     *
     * @param dateTime
     * @return
     */
    public static int getWeekSeconds(String dateTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateFormatUtils.parse(dateTime, GnssFile.DATE_FORMAT));
        int days = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        return days * 24 * 60 * 60 + calendar.get(Calendar.DAY_OF_YEAR) * 60 * 60 + calendar.get(Calendar.MINUTE) * 60 + calendar.get(Calendar.SECOND);
    }
}
